package thread;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.security.DigestInputStream;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/**
 * 获取文件的摘要信息
 * @author Lenovo
 *
 */
public class DigestUtil implements Runnable{
	
	private String fileName;
	private ThreadCallback callback;
	
	public DigestUtil(String fileName, ThreadCallback callback) {
		this.fileName = fileName;
		this.callback = callback;
	}

	public String getFileName() {
		return fileName;
	}

	public void setFileName(String fileName) {
		this.fileName = fileName;
	}

	public ThreadCallback getCallback() {
		return callback;
	}

	public void setCallback(ThreadCallback callback) {
		this.callback = callback;
	}

	public void run() {
		try {
			FileInputStream fis = new FileInputStream(fileName);
			MessageDigest messageDigest = MessageDigest.getInstance("MD5");
			DigestInputStream dis = new DigestInputStream(fis, messageDigest);
			while(dis.read()!=-1);//更新摘要信息
			dis.close();
			byte[] digest = messageDigest.digest();//获取摘要信息
			callback.receiverDigest(digest);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

}
